package com.atguigu.controller;

import com.atguigu.entity.SkuInfo;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Author: TT
 * @Date: 2022/7/28 22:41
 */
@RestController
@RequestMapping("/init")
@Api(tags = "布隆过滤器")
public class BloomFilterController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RBloomFilter<Long> skuBloomFilter;
    //初始化布隆过滤器
    @GetMapping("/sku/bloom")
    public void bloom(){

        skuBloomFilter.delete();
        skuBloomFilter.tryInit(10000,0.001);

        //加载数据库中所有的数据id
        QueryWrapper<SkuInfo> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        List<SkuInfo> list = skuInfoService.list(wrapper);
        //在每次查数据库之前，先查询布隆过滤器里面是否有该数据
        for (SkuInfo skuInfo : list) {
            Long id = skuInfo.getId();
            skuBloomFilter.add(id);
        }
    }
}
